package com.Shiliu.servlet;

import com.Shiliu.pojo.User;
import com.Shiliu.service.UserServiceImpl;
import com.Shiliu.service.userService;
import com.alibaba.fastjson.JSONObject;
import com.mysql.cj.xdevapi.JsonArray;
import com.sun.istack.internal.logging.Logger;
import com.Shiliu.pojo.User;
import com.Shiliu.service.UserServiceImpl;
import com.Shiliu.service.userService;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet {
    userService userService;


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        service(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }



    public static void Login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;utf-8");
        String username=req.getParameter("num");
        String password= req.getParameter("password");
        System.out.println(username);
        System.out.println(password);
        userService userService = new UserServiceImpl();
        boolean flag = false;//默认登录不成功

        User user = userService.loginImpl(username, password);
        System.out.println("准备调用login业务");  //这里已经把登录的人给查出来了
        if (user!=null){ //查有此人，可以登录
            //将用户的信息放到Session中;
            req.getSession().setAttribute("id", user.getId());
            req.getSession().setAttribute("userCode", username);
            req.getSession().setAttribute("password", password);
            System.out.println("user不为空");
            flag=true;
        }else {//查无此人，无法登录
            //转发回登录页面，顺带提示它，用户名或者密码错误；
            System.out.println("查无此人");
            flag=false;
        }

        PrintWriter out = resp.getWriter();
        out.print("龙炫");//返回登录信息
        out.flush();
        out.close();
    }


    public static void MyInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        Integer id =Integer.valueOf(req.getParameter("id"));
        userService userService = new UserServiceImpl();
        User user=userService.getMyInfoImpl(id);
        System.out.println(user.getSex());
        System.out.println(user.getArea());
        JSONObject result=new JSONObject();

        JSONArray array= JSONArray.fromObject(user);
        result.put("user",array);

        System.out.println(array.get(0));
        PrintWriter out = resp.getWriter();
        out.print(result);//返回登录信息

//        resp.setHeader("Content-type","charset=GBK");
        out.flush();
        out.close();

    }

}
